/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aljfce.business.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author jacopo
 */
@Entity
@Table(name = "Event")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Event.findAll", query = "SELECT e FROM Event e"),
    @NamedQuery(name = "Event.findById", query = "SELECT e FROM Event e WHERE e.id = :id"),
    @NamedQuery(name = "Event.findByStartingDate", query = "SELECT e FROM Event e WHERE e.startingDate = :startingDate"),
    @NamedQuery(name = "Event.findByEndingTime", query = "SELECT e FROM Event e WHERE e.endingTime = :endingTime"),
    @NamedQuery(name = "Event.findByLocation", query = "SELECT e FROM Event e WHERE e.location = :location"),
    @NamedQuery(name = "Event.findByOutdoor", query = "SELECT e FROM Event e WHERE e.outdoor = :outdoor"),
    @NamedQuery(name = "Event.findByDescription", query = "SELECT e FROM Event e WHERE e.description = :description")})
public class Event implements Serializable {

    @ManyToMany(mappedBy = "eventCollection")
    private Collection<Calendar> calendarCollection;
    @JoinTable(name = "Event_WeatherInformation", joinColumns = {
        @JoinColumn(name = "Event_Id", referencedColumnName = "Id")}, inverseJoinColumns = {
        @JoinColumn(name = "WeatherInformation_DayTime", referencedColumnName = "DayTime"),
        @JoinColumn(name = "WeatherInformation_Location", referencedColumnName = "Location")})
    @ManyToMany
    private Collection<WeatherInformation> weatherInformationCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "eventId")
    private Collection<Notification> notificationCollection;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "Id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Column(name = "StartingDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date startingDate;
    @Basic(optional = false)
    @NotNull
    @Column(name = "EndingTime")
    @Temporal(TemporalType.TIMESTAMP)
    private Date endingTime;
    @Size(max = 60)
    @Column(name = "Location")
    private String location;
    @Basic(optional = false)
    @NotNull
    @Column(name = "Outdoor")
    private boolean outdoor;
    @Size(max = 45)
    @NotNull
    @Column(name = "Title")
    private String title;
    @Size(max = 240)
    @Column(name = "Description")
    private String description;
    @NotNull
    @Column(name = "Visibility")
    private Boolean visibility;

    @JoinColumn(name = "Creator", referencedColumnName = "Email")
    @ManyToOne(optional = false)
    private User creator;

    public Event() {
        this.visibility = false;
    }

    public Event(Integer id) {
        this.id = id;
    }

    public Event(Integer id, Date startingDate, Date endingTime, boolean outdoor) {
        this.id = id;
        this.startingDate = startingDate;
        this.endingTime = endingTime;
        this.outdoor = outdoor;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Date getStartingDate() {
        return startingDate;
    }

    public void setStartingDate(Date startingDate) {
        this.startingDate = startingDate;
    }

    public Date getEndingTime() {
        return endingTime;
    }

    public void setEndingTime(Date endingTime) {
        this.endingTime = endingTime;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public boolean getOutdoor() {
        return outdoor;
    }

    public void setOutdoor(boolean outdoor) {
        this.outdoor = outdoor;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public User getCreator() {
        return creator;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Event)) {
            return false;
        }
        Event other = (Event) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.aljfce.business.entity.Event[ id=" + id + " ]";
    }

    @XmlTransient
    public Collection<Notification> getNotificationCollection() {
        return notificationCollection;
    }

    public void setNotificationCollection(Collection<Notification> notificationCollection) {
        this.notificationCollection = notificationCollection;
    }

    @XmlTransient
    public Collection<Calendar> getCalendarCollection() {
        return calendarCollection;
    }

    public void setCalendarCollection(Collection<Calendar> calendarCollection) {
        this.calendarCollection = calendarCollection;
    }

    @XmlTransient
    public Collection<WeatherInformation> getWeatherInformationCollection() {
        return weatherInformationCollection;
    }

    public void setWeatherInformationCollection(Collection<WeatherInformation> weatherInformationCollection) {
        this.weatherInformationCollection = weatherInformationCollection;
    }
    
    public WeatherInformation getWeatherInformation(){
        if(weatherInformationCollection == null || weatherInformationCollection.isEmpty())
            return null;
        ArrayList<WeatherInformation> wInf = new ArrayList<WeatherInformation>(weatherInformationCollection);
        return wInf.get(0);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Boolean getVisibility() {
        return visibility;
    }

    public void setVisibility(Boolean visibility) {
        this.visibility = visibility;
    }

}
